package com.server.questionbank.snframe.config;

import com.server.questionbank.snframe.config.interceptor.JwtInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

//import com.server.questionbank.snframe.config.interceptor.HttpOptionsInterceptor;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Value("${spring.security.allowed-origins:}")
    String[] allowedOrigins;

//    @Autowired
//    private HttpOptionsInterceptor optionsInterceptor;

    @Autowired
    private JwtInterceptor jwtInterceptor;

    // 配置跨域请求
//    @Override
//    public void addCorsMappings(org.springframework.web.servlet.config.annotation.CorsRegistry registry) {
//        registry.addMapping("/**")
//                .allowedOrigins("http://localhost:3000/", "http://127.0.0.1:3000/", "http://qb.sso.com:3000/", "http://47.99.143.209:3000", "http://8.134.180.200:3000")  // 设置允许跨域的源
//                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")  // 设置允许的方法
//                .allowedHeaders("*")  // 设置允许的请求头
//                .allowCredentials(true)  // 允许携带凭证
//                .maxAge(3600);  // 设置预检请求的缓存时间
//    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(optionsInterceptor);
        registry.addInterceptor(jwtInterceptor)
                .addPathPatterns("/**")  // 拦截所有请求
                .excludePathPatterns( // 以下接口不拦截
                        "/login/**", // 登录接口
                        "/register/**", // 注册接口
                        "/users/overt/**", // 用户公开信息接口
//                        "/users/updateAvatar",
                        "/code/**", // 验证码接口
                        "/public/**",
                        "/course/**",
                        "/questionban/**",
                        "/deepSeek/**",
                        "/ollama/**",
                        "/course/overt/**"
                );
    }
}